/*
 * Copyright (C) 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.caliper.runner;

import static com.google.common.base.Preconditions.checkState;

import com.google.caliper.model.BenchmarkSpec;
import com.google.caliper.model.Host;
import com.google.caliper.model.Run;
import com.google.caliper.model.Scenario;
import com.google.caliper.model.Trial;
import com.google.caliper.runner.Instrument.MeasurementCollectingVisitor;
import com.google.caliper.runner.ServerSocketService.OpenedSocket;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;

import java.util.UUID;

/**
 * Configuration for a {@link TrialRunLoop}.
 */
final class TrialModule extends AbstractModule {

  @Override protected void configure() {
    install(TrialScopes.module());
  }

  @TrialScoped
  @Provides
  BenchmarkSpec provideBenchmarkSpec(Experiment experiment) {
    return new BenchmarkSpec.Builder()
        .className(experiment.instrumentation().benchmarkMethod().getDeclaringClass().getName())
        .methodName(experiment.instrumentation().benchmarkMethod().getName())
        .addAllParameters(experiment.userParameters())
        .build();
  }

  @Provides
  @TrialScoped
  ListenableFuture<OpenedSocket> provideTrialSocket(
      @TrialId UUID trialId,
      ServerSocketService serverSocketService) {
    return serverSocketService.getConnection(trialId);
  }

  @Provides
  MeasurementCollectingVisitor provideMeasurementCollectingVisitor(Experiment experiment) {
    return experiment.instrumentation().getMeasurementCollectingVisitor();
  }

  @Provides TrialFactory provideTrialFactory(@TrialId final UUID trialId,
      final Run run,
      final Host host,
      final Experiment experiment,
      final BenchmarkSpec benchmarkSpec) {
    return new TrialFactory() {
      @Override public Trial newTrial(VmDataCollectingVisitor dataCollectingVisitor,
          MeasurementCollectingVisitor measurementCollectingVisitor) {
        checkState(measurementCollectingVisitor.isDoneCollecting());
        return new Trial.Builder(trialId)
            .run(run)
            .instrumentSpec(experiment.instrumentation().instrument().getSpec())
            .scenario(new Scenario.Builder()
                .host(host)
                .vmSpec(dataCollectingVisitor.vmSpec())
                .benchmarkSpec(benchmarkSpec))
            .addAllMeasurements(measurementCollectingVisitor.getMeasurements())
            .build();
      }
    };
  }
}
